package project;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class DataManipulation {

	public static String[][] txt2array(String filePath, int inst, int attr) {
		BufferedReader br = null;
		String[][] matrix = new String[inst][attr];
		int i = 0;
		try {
			String sCurrentLine;
			br = new BufferedReader(new FileReader(filePath));
			while ((sCurrentLine = br.readLine()) != null) {
				String[] ArrayCurrentline = sCurrentLine.split(",");
				for (int j = 0; j < attr; j++) {
					matrix[i][j] = ArrayCurrentline[j].toString();
				}
				i++;
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null)
					br.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return matrix;
	}

	public static void printStringArray2d(String[][] matrix) {
		for (int i = 0; i < matrix.length; i++) {
			System.out.print("Instance " + i + ": ");
			for (int j = 0; j < matrix[0].length; j++) {
				System.out.print(matrix[i][j] + " ");
			}
			System.out.println();
			;
		}
	}

	public static void printIntArray2d(int[][] matrix) {
		for (int i = 0; i < matrix.length; i++) {
			Arrays.sort(matrix[i]);
			System.out.print("experiment " + i + ": ");
			for (int j = 0; j < matrix[0].length; j++) {
				System.out.print(matrix[i][j] + " ");
			}
			System.out.println();
			;
		}
	}

	public static void printMode(String[][] matrix) {
		for (int i = 0; i < matrix.length; i++) {
			System.out.print("Mode " + (i + 1) + ": ");
			for (int j = 0; j < matrix[0].length; j++) {
				System.out.print(matrix[i][j] + " ");
			}
			System.out.println();
			;
		}
	}

	public static void printIntArray(int array[]) {
		for (int i = 0; i < array.length; i++) {
			System.out.println("instance " + (i + 1) + " belongs to cluster "
					+ array[i]);
		}
	}

	// compareArrays
	public static boolean compareArrays(int[] array1, int[] array2) {
		boolean b = true;
		if (array1 != null && array2 != null) {
			if (array1.length != array2.length)
				b = false;
			else
				for (int i = 0; i < array2.length; i++) {
					if (array2[i] != array1[i]) {
						b = false;
					}
				}
		} else {
			b = false;
		}
		return b;

	}

	// ------------------------------------------------------------------for
	// experiment
	// module-----------------------------------------------------------------------------------
	// removeDuplicate
	public static Integer[][] removeDuplicate(Integer[][] array) {
		Set<List<Integer>> set = new LinkedHashSet<List<Integer>>();
		for (Integer[] a : array) {
			set.add(Arrays.asList(a));
		}
		Integer[][] temp = new Integer[set.size()][];
		Integer index = 0;
		for (List<Integer> s : set) {
			temp[index++] = s.toArray(new Integer[] {});
		}
		return temp;
	}

	// Convert int[] to Integer[]
	public static Integer[] toObject(int[] intArray) {

		Integer[] result = new Integer[intArray.length];
		for (int i = 0; i < intArray.length; i++) {
			result[i] = Integer.valueOf(intArray[i]);
		}
		return result;
	}

	// Convert Integer[] to int[]
	public static int[] toPrimitive(Integer[] IntegerArray) {

		int[] result = new int[IntegerArray.length];
		for (int i = 0; i < IntegerArray.length; i++) {
			result[i] = IntegerArray[i].intValue();
		}
		return result;
	}

	// Counter
	public static int count(int[] base, int experiment[][]) {
		int count = 0;
		for (int i = 0; i < experiment.length; i++) {
			if (DataManipulation.compareArrays(base, experiment[i])) {
				count++;
			}
		}
		return count;
	}

	public static int count(int base, int array[]) {
		int count = 0;
		for (int i = 0; i < array.length; i++) {
			if (base == array[i]) {
				count++;
			}
		}
		return count;
	}

	public static int count(String base, String array[]) {
		int count = 0;
		for (int i = 0; i < array.length; i++) {
			if (base.equals(array[i])) {
				count++;
			}
		}
		return count;
	}

	// ---------------------------------------------------------------------selection
	// method 2 --------------------------------------
	public static int[] removeDuplicates(int[] array) {

		if (array == null)
			return null;
		if (array.length == 0)
			return new int[0];

		LinkedHashSet<Integer> set = new LinkedHashSet<Integer>();
		for (int n : array)
			set.add(new Integer(n));

		int cursor = 0;
		int[] newArray = new int[set.size()];
		for (Integer n : set)
			newArray[cursor++] = n.intValue();
		return newArray;
	}

	public static String[] removeDuplicatesStr(String[] array) {
		if (array == null)
			return null;
		if (array.length == 0)
			return new String[0];
		LinkedHashSet<String> set = new LinkedHashSet<String>();
		for (String n : array)
			set.add(new String(n));

		int cursor = 0;
		String[] newArray = new String[set.size()];
		for (String n : set)
			newArray[cursor++] = n;
		return newArray;
	}

	// reverse
	public static void reverse(int[] data) {
		for (int i = 0; i < data.length / 2; i++) {
			int temp = data[i];
			data[i] = data[data.length - i - 1];
			data[data.length - i - 1] = temp;
		}
	}

	public static void reverse(String[] data) {
		for (int i = 0; i < data.length / 2; i++) {
			String temp = data[i];
			data[i] = data[data.length - i - 1];
			data[data.length - i - 1] = temp;
		}
	}

}
